package com.shyroke.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.bind.annotation.adapters.XmlAdapter;

import com.shyroke.bean.MyRole;
import com.shyroke.bean.Role;

public class MapAdapter extends XmlAdapter<MyRole[], Map<String, List<Role>>> {

	/**
	 * 适配转换 MyRole[] -> Map<String, List<Role>>
	 */
	@Override
	public MyRole[] marshal(Map<String, List<Role>> roleMap) throws Exception {
		MyRole[] roles = new MyRole[roleMap.size()];

		int i = 0;
		for (String key : roleMap.keySet()) {
			roles[i] = new MyRole();
			roles[i].setKey(key);
			roles[i].setValue(roleMap.get(key));
			i++;
		}

		return roles;
	}

	/**
	 * 适配转换 Map<String, List<Role>> -> MyRole[]
	 */
	@Override
	public Map<String, List<Role>> unmarshal(MyRole[] roles) throws Exception {

		Map<String, List<Role>> map = new HashMap<String, List<Role>>();

		for (int i = 0; i < roles.length; i++) {
			map.put(roles[i].getKey(), roles[i].getValue());
		}

		return map;
	}

}
